package com.das.mechanic_base.adapter.main;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.bean.main.HomeBodyItemBean;
import com.das.mechanic_base.bean.plan.PlanListBean;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3DateUtils;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3LoadingHolder;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.widget.X3MarqueeText;

import java.util.ArrayList;
import java.util.List;

public class X3HomeNewVpAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<HomeBodyItemBean.ListBean> mList;

    private List<ObjectAnimator> oList;

    /**
     * 0 正常
     * 1 空白占位
     * 2 loading
     */
    private int mEmptyType;

    private PlanListBean listBean;

    private String statusName;

    // 所属大项
    private long index;

    private String money_code = "";

    public X3HomeNewVpAdapter(Context mContext, long index, String statusName) {
        this.mContext = mContext;
        this.index = index;
        this.statusName = statusName;
        this.mList = new ArrayList<>();
        this.oList = new ArrayList<>();
        mEmptyType = 2;
    }

    /**
     * 下拉刷新
     * @param mList
     */
    public void changeAdapter(List<HomeBodyItemBean.ListBean> mList) {
        this.mList = mList;
        onDestory();
        money_code = (String) SpHelper.getData(X3Constent.MONEYCODE, "£");
        mEmptyType = X3StringUtils.isListEmpty(mList) ? 1 : 0;
        notifyDataSetChanged();
    }

    /**
     * 上拉加载
     * @param mList
     */
    public void changeAddAdapter(List<HomeBodyItemBean.ListBean> mList) {
        this.mList.addAll(mList);
        onDestory();
        money_code = (String) SpHelper.getData(X3Constent.MONEYCODE, "£");
        mEmptyType = X3StringUtils.isListEmpty(this.mList) ? 1 : 0;
        notifyDataSetChanged();
    }

    /**
     * 取消动画
     */
    public void onDestory() {
        if (X3StringUtils.isListEmpty(oList)) {
            return;
        }
        for (ObjectAnimator objectAnimator : oList) {
            if (objectAnimator == null) {
                continue;
            }
            objectAnimator.cancel();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case 1:
                return new EmptyHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_empty_home_item, parent, false));
            case 2:
                return new X3LoadingHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_home_loading_item, parent, false));
            default:
                return new HomeNewVpHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_home_new_vp_item, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case 0:
                HomeNewVpHolder vpHolder = (HomeNewVpHolder) holder;

                RecyclerView.LayoutParams vpLayout = (RecyclerView.LayoutParams) vpHolder.itemView.getLayoutParams();
                if (position == mList.size() - 1) {
                    vpLayout.topMargin = X3ScreenUtils.dipToPx(12,mContext);
                    vpLayout.bottomMargin = X3ScreenUtils.dipToPx(20,mContext);
                }else {
                    vpLayout.topMargin = X3ScreenUtils.dipToPx(12,mContext);
                    vpLayout.bottomMargin = 0;
                }
                holder.itemView.setLayoutParams(vpLayout);

                vpHolder.v_line1.setVisibility((index == 0) ? View.VISIBLE : View.GONE);
                vpHolder.rl_time.setVisibility((index == 0) ? View.VISIBLE : View.GONE);

                String receiveBaseKey = mList.get(position).receiveBaseKey;
                vpHolder.tv_order_id.setText("#" + ((!X3StringUtils.isEmpty(receiveBaseKey) && receiveBaseKey.length() >= 4) ? receiveBaseKey.substring(receiveBaseKey.length() - 4) : ""));

                List<String> serviceNameList = mList.get(position).serviceNameList;
                if (!X3StringUtils.isListEmpty(serviceNameList)) {
                    if (serviceNameList.size() == 1) {
                        vpHolder.tv_service_name.setText("#" + serviceNameList.get(0));
                    }else {
                        vpHolder.tv_service_name.setText(String.format(mContext.getString(R.string.service_name_more),"#" + serviceNameList.get(0),serviceNameList.size()));
                    }

                }else {
                    vpHolder.tv_service_name.setText(mContext.getString(R.string.x3_no_create_service));
                }

                X3GlideNewUtils.loadNormalImage(mContext, X3StringUtils.getImageUrl(mList.get(position).carBrandResourceUrl),vpHolder.iv_car,R.mipmap.x3_car_icon_no);

                String brandName = X3StringUtils.isEmpty(mList.get(position).carBrandName) ? "－" : mList.get(position).carBrandName;

                String seriesName = X3StringUtils.isEmpty(mList.get(position).carStyleName) ? "" : mList.get(position).carStyleName;

                vpHolder.tv_car.setText(brandName + " " + seriesName);

                vpHolder.tv_car_num.setText(mList.get(position).carNum + "");

                X3GlideNewUtils.loadCircleImage(mContext, X3StringUtils.getImageUrl(mList.get(position).carOwnerUserImgUrl),vpHolder.iv_user,R.mipmap.x3_car_owner_header);

                String carOwnerName = X3StringUtils.isEmpty(mList.get(position).carOwnerName) ? "－" : mList.get(position).carOwnerName;

                vpHolder.tv_user.setText(carOwnerName);

                String carOwnerMobile = X3StringUtils.isEmpty(mList.get(position).carOwnerMobile) ? mContext.getString(R.string.x3_no_bind_phone) : mList.get(position).carOwnerMobile;

                vpHolder.tv_user_num.setText(carOwnerMobile);

                String mgtCreate = mList.get(position).mgtCreate;

                vpHolder.tv_time.setText(mContext.getString(R.string.x3_arrive_store_time) + ": " + X3DateUtils.HomeRelativeTime(mgtCreate,mContext));

                int homeCompare = X3DateUtils.getHomeCompare(mgtCreate);

                if (homeCompare <= 5) {
                    vpHolder.v_stoke.setVisibility(View.VISIBLE);
                    oList.add(getAlphaAnim(vpHolder.v_stoke));
                }else {
                    vpHolder.v_stoke.setVisibility(View.GONE);
                }

                List<HomeBodyItemBean.ListBean.FollowupStaffBaseEntityListBean> staffList = mList.get(position).followupStaffBaseEntityList;

                vpHolder.ll_staff.setVisibility(X3StringUtils.isListEmpty(staffList) ? View.GONE : View.VISIBLE);

                vpHolder.ll_staff_num.removeAllViews();

                if (!X3StringUtils.isListEmpty(staffList)) {
                    if (staffList.size() <= 2) {
                        for (int i = 0; i < staffList.size(); i++) {
                            View view = View.inflate(mContext,R.layout.x3_home_body_header_item,null);
                            ImageView iv_header = view.findViewById(R.id.iv_header);
                            TextView tv_name = view.findViewById(R.id.tv_name);
                            X3GlideNewUtils.loadCircleImage(mContext, X3StringUtils.getImageUrl(staffList.get(i).portrait),iv_header,R.mipmap.x3_car_owner_header);
                            tv_name.setText(staffList.get(i).name + "");
                            vpHolder.ll_staff_num.addView(view);
                        }
                    }else {
                        for (int i = 0; i < staffList.size(); i++) {
                            ImageView iv_header = new ImageView(mContext);
                            LinearLayout.LayoutParams headLayout = new LinearLayout.LayoutParams(X3ScreenUtils.dipToPx(20,mContext), X3ScreenUtils.dipToPx(20,mContext));
                            headLayout.rightMargin = X3ScreenUtils.dipToPx(12,mContext);
                            iv_header.setLayoutParams(headLayout);
                            X3GlideNewUtils.loadCircleImage(mContext, X3StringUtils.getImageUrl(staffList.get(i).portrait),iv_header,R.mipmap.x3_car_owner_header);
                            vpHolder.ll_staff_num.addView(iv_header);
                        }
                    }
                }

                vpHolder.rl_car.setOnClickListener(v -> {
                    if (iOnClickHomeWork != null) {
                        iOnClickHomeWork.iOnClickCar(mList.get(position).carId);
                    }
                });

                vpHolder.rl_user.setOnClickListener(v -> {
                    if (iOnClickHomeWork != null) {
                        iOnClickHomeWork.iOnClickUser(mList.get(position).carOwnerUserId,mList.get(position).carId);
                    }
                });

                vpHolder.btn_status.setVisibility(mList.get(position).payStatus ? View.INVISIBLE : View.VISIBLE);

                vpHolder.btn_status.setOnClickListener(v -> {
                    if (iOnClickHomeWork != null) {
                        iOnClickHomeWork.updateReceiveStatus(mList.get(position).id,position);
                    }
                });

                vpHolder.itemView.setOnClickListener(v -> {
                    if (iOnClickHomeWork != null) {
                        iOnClickHomeWork.iOnClickWorkGetCar(mList.get(position).id);
                    }
                });
                break;
            case 1:
                EmptyHolder emptyHolder = (EmptyHolder) holder;
                emptyHolder.tv_empty.setText(String.format(mContext.getString(R.string.x3_no_status_order),statusName));

                emptyHolder.rl_create.setVisibility(View.VISIBLE);
                emptyHolder.rl_quick.setVisibility(View.VISIBLE);
                if (listBean != null && !X3StringUtils.isEmpty(listBean.serviceBaseName)) {
                    emptyHolder.tv_quick.setText(listBean.serviceBaseName + "");
                }else {
                    emptyHolder.tv_quick.setText(mContext.getString(R.string.x3_quick_alone));
                }
                break;
            case 2:

                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mEmptyType;
    }


    @Override
    public int getItemCount() {
        return X3StringUtils.isListEmpty(mList) ? 1 : mList.size();
    }

    IOnClickHomeWork iOnClickHomeWork;

    public void setiOnClickHomeWork(IOnClickHomeWork iOnClickHomeWork) {
        this.iOnClickHomeWork = iOnClickHomeWork;
    }

    public void changeQuickName(PlanListBean serviceName) {
        this.listBean = serviceName;
        notifyDataSetChanged();
    }

    public interface IOnClickHomeWork {

        // 接车单
        void iOnClickWorkGetCar(long id);

        // 施工单
        void iOnClickWork(long id, String serviceName, long receiveBaseId);

        /**
         * 跳转到已经完成的施工单详情
         *
         * @param id
         * @param serviceName
         * @param workName
         * @param userId
         */
        void iOnClickWorkFinish(int id, String serviceName, String workName, int userId, String carNum);

        // 预约单
        void iOnClickAppoint(int id);

        // 车主详情页
        void iOnClickUser(long carOwnId,long carId);

        // 车辆详情页
        void iOnClickCar(long carId);

        /**
         * 创建接车单
         */
        void iCreateService();

        /**
         * 快速检测
         */
        void iCreateQuick();

        /**
         * 修改接车单的状态
         */
        void updateReceiveStatus(long receiveBaseId,int position);
    }

    class HomeNewVpHolder extends RecyclerView.ViewHolder {
        X3MarqueeText tv_order_id;
        TextView tv_service_name;
        RelativeLayout rl_car;
        ImageView iv_car;
        TextView tv_car;
        TextView tv_car_num;
        ImageView iv_user;
        TextView tv_user;
        TextView tv_user_num;
        LinearLayout ll_staff;
        LinearLayout ll_staff_num;
        TextView tv_time;
        Button btn_status;
        RelativeLayout rl_time;
        View v_line1;
        RelativeLayout rl_user;
        TextView v_stoke;

        public HomeNewVpHolder(@NonNull View itemView) {
            super(itemView);
            tv_order_id = itemView.findViewById(R.id.tv_order_id);
            tv_service_name = itemView.findViewById(R.id.tv_service_name);
            rl_car = itemView.findViewById(R.id.rl_car);
            tv_car_num = itemView.findViewById(R.id.tv_car_num);
            tv_time = itemView.findViewById(R.id.tv_time);
            iv_car = itemView.findViewById(R.id.iv_car);
            tv_car = itemView.findViewById(R.id.tv_car);
            iv_user = itemView.findViewById(R.id.iv_user);
            tv_user = itemView.findViewById(R.id.tv_user);
            tv_user_num = itemView.findViewById(R.id.tv_user_num);
            ll_staff = itemView.findViewById(R.id.ll_staff);
            ll_staff_num = itemView.findViewById(R.id.ll_staff_num);
            btn_status = itemView.findViewById(R.id.btn_status);
            rl_time = itemView.findViewById(R.id.rl_time);
            v_line1 = itemView.findViewById(R.id.v_line1);
            rl_user = itemView.findViewById(R.id.rl_user);
            v_stoke = itemView.findViewById(R.id.v_stoke);


        }
    }

    class EmptyHolder extends RecyclerView.ViewHolder {
        TextView tv_empty;
        RelativeLayout rl_create;
        RelativeLayout rl_quick;
        TextView tv_quick;

        public EmptyHolder(@NonNull View itemView) {
            super(itemView);
            tv_empty = itemView.findViewById(R.id.tv_empty);
            rl_create = itemView.findViewById(R.id.rl_create);
            rl_quick = itemView.findViewById(R.id.rl_quick);
            tv_quick = itemView.findViewById(R.id.tv_quick);

            rl_create.setOnClickListener(v -> {
                if (iOnClickHomeWork != null) {
                    iOnClickHomeWork.iCreateService();
                }
            });
            rl_quick.setOnClickListener(v -> {
                if (iOnClickHomeWork != null) {
                    iOnClickHomeWork.iCreateQuick();
                }
            });
        }
    }

    private ObjectAnimator getAlphaAnim(View view) {
        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(view,"alpha",1,0,1,0,1,0);
        alphaAnim.setDuration(2000);
        alphaAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (view != null) {
                    view.setVisibility(View.GONE);
                }
            }
        });
        alphaAnim.start();
        return alphaAnim;
    }

}
